package com.qzw.utils.mysql.generator;

import lombok.Data;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 代码生成器
 */
@Data
public class Generator{

    private DaoGenerator daoGenerator;
    private ModelGenerator modelGenerator;
    private ServiceGenerator serviceGenerator;
    private ServiceImplGenerator serviceImplGenerator;
    private QueryGenerator queryGenerator;
    private String entityPackage;
    private String basePath;
    private String entityPrefix = "";

    public Generator(String entityPackage) {
        this(entityPackage, "/src/main/java/");
    }

    public Generator(String entityPackage, String basePath) {
        daoGenerator = new DaoGenerator();
        modelGenerator = new ModelGenerator();
        serviceImplGenerator = new ServiceImplGenerator();
        serviceGenerator = new ServiceGenerator();
        queryGenerator = new QueryGenerator();
        this.basePath = basePath;
        this.entityPackage = entityPackage;
    }

    public void setEntityPrefix(String prefix) {
        this.entityPrefix = prefix;
        daoGenerator.setEntityPrefix(prefix);
        modelGenerator.setEntityPrefix(prefix);
        serviceGenerator.setEntityPrefix(prefix);
        serviceImplGenerator.setEntityPrefix(prefix);
        queryGenerator.setEntityPrefix(prefix);
    }


    public void setDaoInit(String packageName, String basePath) {
        daoGenerator.setPackageName(packageName);
        daoGenerator.setBasePath(basePath);
    }

    public void setModelInit(String packageName, String basePath) {
        modelGenerator.setPackageName(packageName);
        modelGenerator.setBasePath(basePath);
    }

    public void setServiceInit(String packageName, String basePath) {
        serviceGenerator.setPackageName(packageName);
        serviceGenerator.setBasePath(basePath);
    }

    public void setServiceImplInit(String packageName, String basePath) {
        serviceImplGenerator.setPackageName(packageName);
        serviceImplGenerator.setBasePath(basePath);
    }

    public void setQueryInit(String packageName, String basePath) {
        queryGenerator.setPackageName(packageName);
        queryGenerator.setBasePath(basePath);
    }

    public void generator() throws Exception {
        File dir = new File(GeneratorUtils.getPath(entityPackage, basePath));
        if (!dir.isDirectory()) {
            return;
        }
        List<Class> entities = new ArrayList<>();
        File[] files = dir.listFiles();
        for (File file : files) {
            try {
                String fileName = file.getName();
                fileName = fileName.substring(0, fileName.lastIndexOf("."));
                entities.add(Class.forName(entityPackage + "." + fileName).newInstance().getClass());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                throw new RuntimeException("entity加载错误");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
        System.out.println(entities);

        daoGenerator.setEntities(entities);
        modelGenerator.setEntities(entities);
        serviceGenerator.setEntities(entities);
        serviceImplGenerator.setEntities(entities);
        queryGenerator.setEntities(entities);

        serviceGenerator.setModelGenerator(modelGenerator);
        serviceGenerator.setQueryGenerator(queryGenerator);
        serviceImplGenerator.setModelGenerator(modelGenerator);
        serviceImplGenerator.setServiceGenerator(serviceGenerator);
        serviceImplGenerator.setDaoGenerator(daoGenerator);
        serviceImplGenerator.setQueryGenerator(queryGenerator);


        daoGenerator.generator();
        modelGenerator.generator();
        serviceGenerator.generator();
        serviceImplGenerator.generator();
        queryGenerator.generator();
    }
}
